/*********The first part of the do file extracts and rename variables from add health raw data wave 1, wave 3, wave 4, wave 4 sec16b, and wave 4sec 16c*************************************/

clear
set more off
set mem 3G

use wave1.dta, clear

/* Note, if using public files, remove the bmi variable from the keep statement.  you will create bmi from the height and weight variables later */

keep aid imonth iyear bmi h1gi1m h1gi1y h1gi4 h1gi6a h1gi6b h1gi6c h1gi6d h1gi6e s1 s2 s3 h1ir1 h1gh59a h1gh59b h1gh60

rename h1gi1m month_birth
rename h1gi1y year_birth
rename h1gi4 hispanic
rename h1gi6a white
rename h1gi6b black
rename h1gi6c AI
rename h1gi6d asian
rename h1gi6e other
rename s1 age_in_year
rename s2 sex
rename s3 grade
rename h1ir1 appearance
rename h1gh59a height_ft
rename h1gh59b height_in
rename h1gh60 weight

gen totheight=height_ft*12+height_in

drop height_ft height_in weight

save wave1info.dta, replace

use "wave3.dta", clear

keep  aid iyear3 h3hr7a h3hr8a h3hr10a h3hr7b h3hr8b h3hr10b h3hr7c h3hr8c h3hr10c h3hr7d h3hr8d h3hr10d h3hr7e h3hr8e h3hr10e h3hr7f h3hr8f h3hr10f h3hr7g h3hr8g h3hr10g h3mr1 h3mr_m_a h3mr2m_a h3mr2y_a h3mr3_a h3mr5m_a h3mr5y_a h3mr_m_b h3mr2m_b h3mr2y_b h3mr3_b h3mr5m_b h3mr5y_b h3mr_m_c h3mr2m_c h3mr2y_c h3mr3_c h3mr5m_c h3mr5y_c h3ir1 h3ir2 h3ir3 

rename h3hr7a sex_1
rename h3hr8a age_1
rename h3hr10a relation_1
rename h3hr7b sex_2
rename h3hr8b age_2
rename h3hr10b relation_2
rename h3hr7c sex_3
rename h3hr8c age_3
rename h3hr10c relation_3
rename h3hr7d sex_4
rename h3hr8d age_4
rename h3hr10d relation_4
rename h3hr7e sex_5
rename h3hr8e age_5
rename h3hr10e relation_5
rename h3hr7f sex_6
rename h3hr8f age_6
rename h3hr10f relation_6
rename h3hr7g sex_7
rename h3hr8g age_7
rename h3hr10g relation_7
rename h3mr1 number_marriages
rename h3mr_m_a idmar1
rename h3mr2m_a month_mar1
rename h3mr2y_a year_mar1
rename h3mr3_a still_mar1
rename h3mr5m_a month_mar1end
rename h3mr5y_a year_mar1end
rename h3mr_m_b idmar2
rename h3mr2m_b month_mar2
rename h3mr2y_b year_mar2
rename h3mr3_b still_mar2
rename h3mr5m_b month_mar2end
rename h3mr5y_b year_mar2end
rename h3mr_m_c idmar3
rename h3mr2m_c month_mar3
rename h3mr2y_c year_mar3
rename h3mr3_c still_mar3
rename h3mr5m_c month_mar3end
rename h3mr5y_c year_mar3end
rename h3ir1 appearance_w3
rename h3ir2 personality_w3
rename h3ir3 grooming_w3

save wave3info.dta,replace

use "wave4.dta", clear

keep  aid iyear4 h4tr1 h4hr5a h4hr6a h4hr8a h4hr5b h4hr6b h4hr8b h4hr5c h4hr6c h4hr8c h4hr5d h4hr6d h4hr8d h4hr5e h4hr6e h4hr8e h4hr5f h4hr6f h4hr8f h4hr5g h4hr6g h4hr8g h4gh6 h4gh5f h4gh5i h4bmi h4ir1 h4ir2 h4ir3 h4ed1 h4ed2

rename h4tr1 number_marriages_w4
rename h4hr5a sexw4_1
rename h4hr6a agew4_1
rename h4hr8a relationw4_1
rename h4hr5b sexw4_2
rename h4hr6b agew4_2
rename h4hr8b relationw4_2
rename h4hr5c sexw4_3
rename h4hr6c agew4_3
rename h4hr8c relationw4_3
rename h4hr5d sexw4_4
rename h4hr6d agew4_4
rename h4hr8d relationw4_4
rename h4hr5e sexw4_5
rename h4hr6e agew4_5
rename h4hr8e relationw4_5
rename h4hr5f sexw4_6
rename h4hr6f agew4_6
rename h4hr8f relationw4_6
rename h4hr5g sexw4_7
rename h4hr6g agew4_7
rename h4hr8g relationw4_7
rename h4gh6 weight
rename h4gh5f height_ft_w4
rename h4gh5i height_in_w4
rename h4bmi bmi_w4
rename h4ir1 appearance_w4
rename h4ir2 personality_w4
rename h4ir3 grooming_w4

save wave4info.dta, replace

use sect16b.dta, clear

keep aid ptnr_id h4tr13 h4tr14 h4tr20 h4tr21 h4tr22

rename h4tr13 type_relation
rename h4tr14 current_mar
*rename h4tr20 age_spouse
rename h4tr21 younger_older
rename h4tr22 age_diff

save wave4info1.dta,replace

use "sect16c.dta", clear

keep aid ptnr_id h4tr25 h4tr26 h4tr27y h4tr28y

rename h4tr25 type_relation_c
rename h4tr26 time_w4
rename h4tr27y year_mar_w4
rename h4tr28y year_marend_w4

save wave4info2.dta,replace



/***********************************************merging waves 1+3+4+wegihts from wave 1*********************************************/

use wave1info.dta, clear

merge 1:1 aid using wave3info.dta

drop _merge

merge 1:1 aid using wave4info.dta

drop _merge

save master2.dta,replace

/** weights1.dta just contains the individual identifier (aid) and grand sample weights wave 1 (gswgt1) renamed as wgt **/

merge 1:1 aid using weights1.dta

drop _merge

save master2.dta, replace




/************************************************************************creating marital histories*******************************/

use "wave4info1.dta", clear

sort aid
keep if type_relation==1 /**keeping marriage relations**/
save "wave4info1.dta", replace

use "wave4info2.dta", clear

sort aid

keep if  type_relation_c==1 /**keeping marriage relations**/
save "wave4info2.dta", replace


use "wave4info1.dta", clear

merge m:m aid ptnr_id using "wave4info2.dta"

drop _merge

save "wave4_marital.dta",replace

keep if ptnr_id==1  /** keeping first marriages**/
keep aid current_mar h4tr20 year_mar_w4 year_marend_w4

save "wave4_marital.dta", replace


use "master2.dta", clear

merge m:m aid using "wave4_marital.dta"

drop _merge

save "Couples_addhealth",replace

drop if year_birth==.
gen year_birth1=1900+year_birth

drop if current_mar==.

gen spouse_birthy1=iyear4-h4tr20

gen agediff1=year_birth1-spouse_birthy1

gen age_mar1=year_mar_w4-year_birth1

save "Couples_addhealth",replace
























